package com.changgou.goods.service.impl;

import com.changgou.goods.dao.BrandMapper;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @author BAO
 * @version V1.0
 * @Package com.changgou.goods.service.impl
 * @date 2020/12/6 22:24
 * @description:
 */
@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Override
    public List<Brand> findAll() {
        return brandMapper.selectAll();
    }

    @Override
    public Brand findById(Integer brandId) {
        return brandMapper.selectByPrimaryKey(brandId);
    }

    @Override
    public int save(Brand brand) {
        return brandMapper.insertSelective(brand);
    }

    @Override
    public int update(Brand brand) {
        return brandMapper.updateByPrimaryKeySelective(brand);
    }

    @Override
    public int deleteById(Integer brandId) {
        return brandMapper.deleteByPrimaryKey(brandId);
    }

    @Override
    public List<Brand> findCondition(Map<String, Object> searchMap) {
        Example example = new Example(Brand.class);
        if (searchMap!=null){
            if (searchMap.get("name")!=null&&!"".equals(searchMap.get("name"))){
                example.createCriteria().andLike("name","%"+searchMap.get("name")+"%");
            }
            if (searchMap.get("letter")!=null&&!"".equals(searchMap.get("letter"))){
                example.createCriteria().andEqualTo("letter",searchMap.get("letter"));
            }
        }
        return brandMapper.selectByExample(example);
    }

    @Override
    public Page<Brand> findByPage(int currentPage, int size) {
        Page<Brand> page = new Page<>(currentPage, size);
        PageHelper.startPage(currentPage, size);
        Page<Brand>  brandList = (Page<Brand>) brandMapper.selectAll();
        //使用pagehelp的详细分页类pageinfo
        //PageInfo<Brand> brandPageInfo = new PageInfo<>(brandList);
        return brandList;
    }

    @Override
    public Page<Brand> findByPageAndCondition(Map<String, Object> searchMap, int currentPage, int size) {
        Example example = new Example(Brand.class);
        if (searchMap!=null){
            if (searchMap.get("name")!=null&&!"".equals(searchMap.get("name"))){
                example.createCriteria().andLike("name","%"+searchMap.get("name")+"%");
            }
            if (searchMap.get("letter")!=null&&!"".equals(searchMap.get("letter"))){
                example.createCriteria().andEqualTo("letter",searchMap.get("letter"));
            }
        }
        PageHelper.startPage(currentPage,size);
        return (Page<Brand>)brandMapper.selectByExample(example);
    }

    @Override
    public List<Map> findListByCategoryName(String categoryName) {
        return brandMapper.findListByCategoryName(categoryName);
    }
}
